package com.xiaomi.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 字符集过滤器
 *
 * @ClassName CharacterEncodingFilter
 * @Author jy
 * @Date 2021/5/22
 */
@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //拦截所有请求并设置字符集
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html; charset=UTF-8");
        //放行
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
